<?php

namespace app\admin\model\system\admin;

use app\customer\curd\CreateInterface;
use app\customer\curd\DeleteInterface;
use app\customer\curd\DetailInterface;
use app\customer\curd\UpdateInterface;
use app\consts\Code;
use app\exception\BizException;
use app\model\admin\Admin;

class AdminRow extends Admin implements DetailInterface,DeleteInterface,CreateInterface,UpdateInterface
{
    //验证器规则
    public function rules() :array
    {
        return [
            'page'              => 'min:1|max:100',
            'page_size'         => 'min:1|max:100',
        ];
    }

    //验证器消息提示
    public function messages() :array
    {
        return [];
    }

    public function detail($id)
    {
        return static::first($id);
    }

    public function bizDelete($id): bool
    {
        $find = $this->detail($id);
        if(!$find){
            throw new BizException(Code::BIZ_RECORD_NOT_FOUND);
        }
        return $find->delete();
    }

    public function bizUpdate($id, $attrs) :bool
    {
        $find = $this->detail($id);
        if(!$find){
            throw new BizException(Code::BIZ_RECORD_NOT_FOUND);
        }
        $fields = [
            //TODO 可以更新的字段
        ];
        return $find->update(collect($attrs)->only($fields)->toArray());
    }

    public function bizCreate(array $attr)
    {
        return static::create($attr);
    }

    //返回值统一字段名
    public function toArray() :array
    {
        return [
            "id" => $this->id,
        ];
    }
}